package com.jxc.service.acegi;

import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.jxc.model.po.Role;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VRole;
import com.jxc.service.impl.ResourceServiceImpl;
import com.jxc.util.ResourceUtil;

public class SecurityTag extends BodyTagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 标签属性begin
	private String key = null;

	/* 标签初始方法 */
	public int doStartTag() throws JspTagException {
		SessionInfo sessionInfo = (SessionInfo) pageContext.getSession()
				.getAttribute(ResourceUtil.getSessionInfoName());
		if (sessionInfo.getLoginName().equals("admin")) {// admin用户不需要验证权限
			return EVAL_BODY_INCLUDE;
		}
		Map<String, List<VRole>> cofig = (Map<String, List<VRole>>) pageContext
				.getServletContext().getAttribute(
						ResourceServiceImpl.SECURITY_CONFIG);
		List<VRole> ro = cofig.get(key);
		if (ro != null){
			for (VRole role : ro) {
				if (role.getId().toString().equals(sessionInfo.getRoleIds())) {
					return EVAL_BODY_INCLUDE;
				}
			}
		}
		return SKIP_BODY;
	}

	/* 释放资源 */
	public void release() {
		super.release();
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

}
